﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using iHub.App_Code;

namespace iHub
{
    public partial class login : Form
    {
        public string loggedUsername;
        public static login log = null;
        public static Boolean locked = false;
        private Boolean notterminated = false;
        private int numdump = 0;
        private AccountManager acct = new AccountManager();

        public login()
        {
            InitializeComponent();
            this.FormClosing += new FormClosingEventHandler(Start_Closing);
        }

        public string logGetUsername()
        {
            return loggedUsername;
        }


        private void login_Load(object sender, EventArgs e)
        {

        }

        private void loginButton_Click(object sender, EventArgs e)
        {
            String user = username.Text;
            String pass = password.Text;
            Boolean type = false;
            DateTime date = DateTime.Now;
            String dt = date.ToShortDateString();
            String time = date.ToShortTimeString();
            Account a = new Account();
            StringValidator validator = new StringValidator();
            int allowed = 1;

            try
            {
                notterminated = acct.isTerminated(user);

                a.setUsername(user);
                a.setPass(pass);

                allowed = acct.verifyLogin(a);

                if (validator.IsSafeString(user) && validator.IsSafeString(pass))
                {
                    if (allowed == 0 && notterminated)
                    {
                        DialogResult msg = MessageBox.Show("You are now logged in as " + user + ". Please make sure to log out after use to prevent unauthorized access.", "Welcome!", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        loggedUsername = username.Text;

                        username.Text = "";
                        password.Text = "";
                        log = this;
                        this.Hide();

                        acct.setAccount(a);
                        acct.resetAttempts(user);
                        type = a.getUsertype();
                        acct.updateLoginHistory(user, type, date);
                        main view = new main(a, dt, time);
                        view.ShowDialog();
                    }
                    else if (allowed != 0 || !notterminated)
                    {
                        if (allowed != 0)
                        {
                            DialogResult msg = MessageBox.Show("Invalid username or password. ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                            username.Text = "";
                            password.Text = "";
                            username.Focus();

                            if (allowed == 1)
                            {
                                acct.incAttempts(user);
                                numdump = acct.getAttempts(a);

                                if (numdump >= 5)
                                {
                                    MessageBox.Show("This account will be terminated for continuously entering invalid credentials", "Account Terminated", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                                    acct.terminateAccount(user);
                                }
                            }
                        }
                        else if (!notterminated)
                        {
                            MessageBox.Show("This account has been terminated, please contact an administrator to reactivate this account ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                            username.Text = "";
                            password.Text = "";
                            username.Focus();
                        }
                    }
                }
                else
                {
                    MessageBox.Show("There are illegal characters present in the text", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    username.Text = "";
                    password.Text = "";
                    username.Focus();
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Start_Closing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                Application.Exit();
            }
        }

        private void forget_Click(object sender, EventArgs e)
        {
            this.Dispose();

            forgetStart view = new forgetStart();
            view.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult msg = MessageBox.Show("Programmed by: GAEL Productions\n © 2011", "GAEL © 2011", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
